package com.shengyin.ec.controller.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.GoodsType;
import com.shengyin.ec.service.alliance.AllianceService;
import com.shengyin.ec.service.goods.GoodsService;
import com.shengyin.ec.service.goods.GoodsTypeService;

@Controller
public class IndexController {
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	GoodsTypeService goodsTypeService;
	
	@Autowired
	AllianceService allianceService;
	
	@RequestMapping("/toIndex.do")
	public String toIndex(Model model){
		List<Goods> goodsList= goodsService.getAllGoodsList();
		List<GoodsType> goodTypeList =goodsTypeService.getGoodsTypeList(0, 5);
		List<Company> companyList=allianceService.getCompanyList(0, 10);
		List<Goods> goodHotSaleList= goodsService.getGoodsList(1, 4);
		List<Goods> goodShow= goodsService.getGoodsList(5, 1);
		List<Goods> glassShow= goodsService.getGoodsListByType("草花", 0, 4);
		List<Goods> penjingShow= goodsService.getGoodsListByType("盆栽", 0, 4);
		List<Goods> seedShow= goodsService.getGoodsListByType("种籽/种苗", 0, 4);
		List<Goods> flowerShow= goodsService.getGoodsListByType("花卉", 0, 5);	
		List<Goods> treeShow= goodsService.getGoodsListByType("苗木", 2, 5);
		//List<Goods> toolsShow= goodsService.getGoodsListByType("设施资材", 2, 5);
		model.addAttribute("companyList", companyList);
		model.addAttribute("goodTypeList", goodTypeList);
		model.addAttribute("goodsList", goodsList);
		model.addAttribute("goodHotSaleList", goodHotSaleList);
		model.addAttribute("goodShow", goodShow);
		model.addAttribute("glassShow", glassShow);
		model.addAttribute("penjingShow", penjingShow);
		model.addAttribute("seedShow", seedShow);
		model.addAttribute("flowerShow", flowerShow);
		model.addAttribute("treeShow", treeShow);
		//model.addAttribute("toolsShow", toolsShow);
		return "/index";
	}
	  /*
     * spring mvc异常处理方法，在controller发生异常，可以以抛出ControllerException，以供捕获
     * */
    @ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  
}
